using System;
using System.Web.Mvc;
using RMH.Entities;
using RMH.Services;
using RMH.Web.Models.Binders;

namespace RMH.Web.Controllers
{
    public class PatientController : ControllerBase
    {
        private IPatientService patientService;
        private ICountyService countyService;
        private readonly INoteService noteService;

        public PatientController(IPatientService patientService, ICountyService serviceCounty, INoteService noteService)
        {
            this.patientService = patientService;
            this.countyService = serviceCounty;
            this.noteService = noteService;
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        public ViewResult Details(int? id)
        {
            var patient = patientService.Get(id.Value);
            string countyName = patient.County != null ? patient.County.Name : string.Empty;
            ViewData["CountyName"] = countyName;
           
            return View(patient);
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult Edit(int? id)
        {
            Patient patient = new Patient();

            if (!id.HasValue)
            {
            }
            else
            {
                patient = patientService.Get(id.Value);
            }

            ViewData["counties"] = new SelectList(countyService.GetAll(), "Id", "Name", patient.County != null ? patient.County.Id : 0);

            return View(patient);
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([ModelBinder(typeof(PatientBinder))] Patient patient)
        {
            if (patient.FirstName == null || String.IsNullOrEmpty(patient.FirstName.Trim()))
            {
                ModelState.AddModelError("FirstName", "First Name is required.");
                ModelState.SetModelValue("FirstName", ValueProvider["FirstName"]);
            }
            if (patient.LastName == null || String.IsNullOrEmpty(patient.LastName.Trim()))
            {
                ModelState.AddModelError("LastName", "Last Name is required.");
                ModelState.SetModelValue("LastName", ValueProvider["LastName"]);
            }
            if (patient.Address == null || String.IsNullOrEmpty(patient.Address.Street1))
            {
                ModelState.AddModelError("AddressStreet1", "Address Street is required.");
                ModelState.SetModelValue("AddressStreet1", ValueProvider["AddressStreet1"]);
            }
            if (patient.Address == null || String.IsNullOrEmpty(patient.Address.City))
            {
                ModelState.AddModelError("AddressCity", "Address City is required.");
                ModelState.SetModelValue("AddressCity", ValueProvider["AddressCity"]);
            }
            if (patient.Address == null || String.IsNullOrEmpty(patient.Address.State))
            {
                ModelState.AddModelError("AddressState", "Address State is required.");
                ModelState.SetModelValue("AddressState", ValueProvider["AddressState"]);
            }
            if (patient.Address == null || String.IsNullOrEmpty(patient.Address.PostalCode))
            {
                ModelState.AddModelError("AddressPostalCode", "Address Postal Code is required.");
                ModelState.SetModelValue("AddressPostalCode", ValueProvider["AddressPostalCode"]);
            }
            else if (patient.Address.PostalCode.Trim().Length < 5)
            {
                ModelState.AddModelError("AddressPostalCode", "Address Postal Code is invalid.");
                ModelState.SetModelValue("AddressPostalCode", ValueProvider["AddressPostalCode"]);
            }
            if (!ModelState.IsValid)
            {
                patientService.Evict(patient);

                ViewData["counties"] = new SelectList(countyService.GetAll(), "Id", "Name");

                return View(patient);
            }
            // HACK: probably don't want to do this, need relationship details
            // to be actually populated though.
            patientService.Save(patient);            
            
            return View("Details", patientService.Get(patient.Id));
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult Notes(int? id)
        {
            Patient patient;
            if (!id.HasValue)
            {
                patient = new Patient();
            }
            else
            {
                patient = patientService.Get(id.Value);
            }
            return View(patient);
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Notes([ModelBinder(typeof(NoteBinder))] Note note)
        {
            if (String.IsNullOrEmpty(note.Body.Trim()))
            {
                ModelState.AddModelError("Note", "Note is required.");
                ModelState.SetModelValue("Note", ValueProvider["Note"]);
            }
            if (!ModelState.IsValid)
            {
                noteService.Evict(note);
                return View(patientService.Get(note.Patient.Id));
            }
            noteService.Save(note);
            return RedirectToAction("Index");
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ViewResult List(string firstName, string lastName)
        {
            var patients = patientService.Search(firstName, lastName);

            if (patients.Count.Equals(1))
                return View("Details", patients[0]);

            return View(patients);
        }

       
    }
}
